package mybitset;

import java.util.Arrays;
import java.util.List;

public class MyBitSet {
    private int useSize;
    private byte[] bytes = new byte[3];

    public MyBitSet(){
    }

    public MyBitSet(int val){
        bytes = new byte[val / 8 + 1];
    }

    //扩容
    private void expandCapacity(byte[] arr,int val){
        bytes = Arrays.copyOf(arr, val/8 + 1);
    }
    //添加数据
    public void set(int val){

        if(val/8 + 1 > bytes.length){
            expandCapacity(bytes,val);
        }

        int index = val / 8;
        int offset = val % 8;
        bytes[index] = (byte) (bytes[index] | (1 << offset));
        useSize++;
    }
    //查看是否存在这个数据
    public boolean get(int val){
        if(val/8 + 1 > bytes.length){
            return false;
        }

        int index = val / 8;
        int offset = val % 8;
        return (bytes[index] & (1 << offset)) != 0;
    }
    //删除这个数据
    public void reSet(int val){
        if(val/8 + 1 > bytes.length){
            return;
        }
        int index = val / 8;
        int offset = val % 8;
        bytes[index] = (byte) (bytes[index] & (~(1 << offset)));
        useSize--;
    }
    //获取总共存储了多少个数据
    public int size(){
        return useSize;
    }
    //打印
    public void print(){
        for(int i = 0;i < bytes.length;i++){
            for(int offset = 0;offset < 8;offset++){
                if((bytes[i] & (1 << offset)) != 0){
                    System.out.print(i * 8 + offset + " ");
                }
            }
        }
    }
}
